package controllers;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import models.Categoria;
import models.Competicao;
import models.Usuario;



public class inserirCompeticao extends Application {

	private static Connection conexao;
	private static Statement comando;
	
	
	
	/***********************************************************************************/
	
	protected static void conectar() throws SQLException {  
	    		  
		try {
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
		}

			conexao = DriverManager.getConnection(  
			        "jdbc:postgresql://localhost:5432/SGRv2",  
			        "postgres",  
			        "senha");
		
			comando =  conexao.createStatement();
	}

    /***********************************************************************************/
	
	public static void inserirCompeticao(String valor) {
		
		render(valor);
	}

	/***********************************************************************************/
	
	public static void inserir( String nome) throws SQLException
	{
		
		String valor = "nome";	
		
		if( nome.isEmpty())
		{
			valor = "O campo 'título da competição' não pode ser vazio.";
			inserirCompeticao(valor);
		}
		else
		{
			conectar();
			
			comando.executeUpdate("insert into competicao (" +
			"nome " +
	        ") values (" +
	        "'" + nome + "' " +
			")");
			
			
			int projid = 0;
			{
			ResultSet reader = comando.executeQuery("select max(id) from competicao");
	    	 
			 if (reader.next())
		        {         
		                projid = ((int)reader.getInt("max"));
		        }
			}
			
			 if(projid > 0)
			 {
				 for(int x = 1; x < 11; x++ )
				 {
				 
				 comando.executeUpdate("insert into rodada (" +
						 
							"nomerodada, " +
							"idcompeticao " +
							
					        ") values (" +
					        
					        "'rodada " + x + "', " +
					        "' " + projid + "' " +
							")");
				 }
				 
				 ResultSet reader2 = comando.executeQuery("select * from rodada where idcompeticao = " + projid);

				 int rodid = 0;
				
				 List<Integer> rodadas = new ArrayList<Integer>();

				 while (reader2.next())
			        {         
					 rodadas.add( ((int)reader2.getInt("id")));
			        }
		
				 for(int r : rodadas) {
		             
					 for(int z = 1; z < 3; z++)
					 {
					 comando.executeUpdate("insert into regata (" +
							 
								"idrodada, " +
								"nomeregata " +
								
						        ") values (" +
						        
						        "' " + r + "', " +
						        "'regata " + z + "' " +
						        
								")");					 
					 }					 
				 
			 }
		}
			
			
			
			
			inserirCompeticao(""  );
		}
	}
	
	/*************************************************************/


}
